package api_io

import (
	"encoding/json"
	"github.com/astaxie/beego/context"
)

type Empty struct{}

//解析客户端请求参数
func Input(input *context.BeegoInput, parser interface{}) (bool, map[string]interface{}) {
	err := json.Unmarshal(input.RequestBody, &parser)
	if err != nil {
		return false, Fail(ErrParamsError, err.Error(), nil)
	}
	return true, nil
}

//响应客户端请求
func out(code int, data interface{}, msg string) map[string]interface{} {
	if data == nil {
		data = &Empty{}
	}
	return map[string]interface{}{
		"Code": code,
		"Data": data,
		"Msg":  msg,
	}
}

//成功
func Success(data interface{}, msg string) map[string]interface{} {
	return out(0, data, msg)
}

//失败
func Fail(code int, msg string, data interface{}) map[string]interface{} {
	return out(code, data, msg)
}
